package com.mdp.workflow.re.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.service.BaseService;
import com.mdp.workflow.re.entity.ProcdefParames;
import com.mdp.workflow.re.entity.ProcdefTag;
import com.mdp.workflow.re.mapper.ProcdefParamesMapper;
import com.mdp.workflow.re.vo.ProcdefParamesVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 父类已经支持增删改查操作,因此,即使本类什么也不写,也已经可以满足一般的增删改查操作了.<br> 
 * 组织 com.mdp  顶级模块 workflow 大模块 re 小模块 <br>
 * 实体 ProcdefParames 表 mdp_re_procdef_parames 当前主键(包括多主键): id; 
 ***/
@Service
public class ProcdefParamesService extends BaseService<ProcdefParamesMapper,ProcdefParames> {
	static Logger logger =LoggerFactory.getLogger(ProcdefParamesService.class);

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}

	@Autowired
	ProcdefNodeInfoService procdefNodeInfoService;

	@Autowired
	ProcdefTagService procdefTagService;


	/**
	 * 更新tagIds,tagNames两个字段为最新的值
	 * @param procDefId
	 * @param tagIds
	 * @param tagNames
	 */
	public void updateTagsByProDefId(String branchId,String procDefId, String tagIds, String tagNames) {
 		Map<String,Object> p=new HashMap<>();
		p.put("procDefId", procDefId);
		p.put("tagIds", tagIds);
		p.put("tagNames", tagNames);
		p.put("branchId", branchId);
		baseMapper.updateTagsByProDefId( p);

	}
	@Transactional
	public void add(ProcdefParamesVo procdefParames) {ProcdefParames ps=procdefParames.getProcdefParames();
		ps.setId(this.createKey("id"));
		this.insert(ps);
		procdefTagService.batchInsertTags(ps.getTagIds(), ps.getTagNames(), ps.getBranchId(), ps.getUserid(), ps.getProcDefId());
		procdefNodeInfoService.addNodeInfos( ps.getProcDefId(),ps.getBranchId(), procdefParames.getNodeInfos());
 	}
	@Transactional
	public void edit(ProcdefParamesVo procdefParames) {

		ProcdefParames ps=procdefParames.getProcdefParames();
		String tagIds=ps.getTagIds();
		String tagNames=ps.getTagNames();
		if("1".equals(procdefParames.getChangeTags())) {
			if(!StringUtils.isEmpty(tagIds) && !StringUtils.isEmpty(tagNames) ) {
				String[] tagIdsArr=tagIds.split(",");
				String[] tagIdsNamesArr=tagNames.split(",");
				if(tagIdsArr.length==tagIdsNamesArr.length) {
					List<ProcdefTag> tags=new ArrayList<>();
					for (String tagId : tagIdsArr) {
						ProcdefTag procdefTag =new ProcdefTag();
						procdefTag.setAddUserid(ps.getUserid());
						procdefTag.setProcDefId(ps.getProcDefId());
						procdefTag.setBranchId(ps.getBranchId());
						procdefTag.setTagId(tagId);
						procdefTag.setId(this.createKey("id"));
					}
					procdefTagService.batchInsertOrDeleteTags(ps.getBranchId(), ps.getProcDefId(), ps.getUserid(), tags);
				}
			}
		}
		this.updateByPk(procdefParames.getProcdefParames());
		procdefNodeInfoService.updateNodeInfos(ps.getProcDefId(),ps.getBranchId(), procdefParames.getNodeInfos());

	}
}

